package com.zxy.servlet;

import java.io.IOException;
import java.io.OutputStream;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.zxy.cache.Root_API_Cache;
import com.zxy.common.Com_Fun;
import com.zxy.common.Com_Para;

public class RootVue extends HttpServlet
{
	public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
	{
		doPost(request,response);
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
	{		
		String AbsProjPath = "";
		//http地址
		String web_path = request.getSession().getServletContext().getRealPath("");
		//file地址
		String file_path = Com_Fun.GetHashTable(Com_Para.ht_T_PARAM_VALUE,"web_path");
		String AbsWebPath = request.getServletPath().replace("/root_api/","/");
		String vue_file = AbsWebPath;
		String[] AbsWebPaths = AbsWebPath.split("/");
		AbsWebPath = "";
		for(String strAb : AbsWebPaths){
			if(!java.util.Objects.equals(strAb,"")){
				AbsWebPath += Com_Para.zxyPath+strAb;
			}
		}
		if(java.util.Objects.equals(file_path,"")){
			AbsProjPath = web_path;
		}
		else{
			AbsProjPath = file_path;
		}
		StringBuilder sb = new StringBuilder();
		if(Root_API_Cache.Vue_Cache.size() > 0){
			vue_file = vue_file.replace("\\","/");
			if(vue_file.indexOf("/") == 0)
				vue_file = vue_file.substring(1,vue_file.length());
			String vue_cont = Com_Fun.GetHashTable2(Root_API_Cache.Vue_Cache,vue_file).replace("\\$","\\\"").replace("\"","\\\"").replace("\t","").replace("\n","").replace("\r","");
			if(java.util.Objects.equals(vue_cont,null) || java.util.Objects.equals(vue_cont,"")){
				vue_cont = "<div class=\"middle-box text-center animated fadeInDown\"><h1>404</h1><h3 class=\"font-bold\">页面未找到！</h3><div class=\"error-desc\">抱歉，页面好像去火星了~</div></div>";
				vue_cont = vue_cont.replace("\\$","\\\"").replace("\"","\\\"").replace("\t","").replace("\n","").replace("\r","");
			}
			sb.append(vue_cont);
		}
		else
			sb = Com_Fun.readVueFile(AbsProjPath.replace("\\","/")+AbsWebPath.replace("\\","/"));		
		response.setContentType("text/javascript");
		request.setCharacterEncoding("utf-8");
		OutputStream out = null;
		try
		{
			out = response.getOutputStream();
			out.write("ReadCommonRes(\"".getBytes(Com_Para.U_CODE));
			out.write(sb.toString().getBytes(Com_Para.U_CODE));
			out.write("\");".getBytes(Com_Para.U_CODE));
			out.flush();
			out.close();
		}
		catch(Exception ex)
		{
		}
		finally
		{
			if(out != null)
				out.close();
		}
	}

	public void init() throws ServletException
	{
		super.init();
	}
}
